package com.hitown.controller.dxhd;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitown.model.dxhd.DxhdAbry;
import com.hitown.model.dxhd.DxhdRybs;
import com.hitown.service.dxhd.DxhdAbryService;
import com.hitown.util.Params;
import com.hitown.util.R;
import com.hitown.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;

/**
 * <p>
 * 安保人员 前端控制器
 * </p>
 *
 * @author menu
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/dxhd-abry")
public class DxhdAbryController {

    @Autowired
    private DxhdAbryService dxhdAbryService;
    
    @ApiOperation(value = "分页查询")
    @RequestMapping("selectPage")
    public R selectPage(@ApiIgnore @RequestBody Params<DxhdAbry> params){
        Page<DxhdAbry> page = new Page<>(params.getCurrent(),params.getSize());
        QueryWrapper<DxhdAbry> wrapper =new QueryWrapper<>();
        if(!StringUtils.isEmpty(params.getEntity().getXm())){
            wrapper.like("XM",params.getEntity().getXm());
        }
        if(!StringUtils.isEmpty(params.getEntity().getZjhm())){
            wrapper.like("ZJHM",params.getEntity().getZjhm());
        }

        wrapper.orderByDesc("GXSJ");
        IPage<DxhdAbry> pageVo = dxhdAbryService.page(page,wrapper);
        for (DxhdAbry e: pageVo.getRecords()) {
            if(e.getZp()!=null){
                String base64Str = "data:image/jpg;base64,"+DatatypeConverter.printBase64Binary(e.getZp());
                e.setZpDz(base64Str);
            }
        }
        return R.ok().put("data",pageVo);

    }


    @ApiOperation(value = "根据id查询记录")
    @RequestMapping("getById")
    public R getById(@RequestParam String Id){
        DxhdAbry result = dxhdAbryService.getById(Id);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "保存单条记录")
    @PostMapping("save")
    public R save(@RequestBody DxhdAbry entit){
        String xxbz = entit.getXxbz();
        if(StringUtils.isEmpty(xxbz)){
            entit.setXxbz(UUID.randomUUID().toString());
        }
        Boolean result = dxhdAbryService.save(entit);
        return R.ok().put("data",result);
    }


    @ApiOperation(value = "批量保存")
    @RequestMapping("saveBatch")
    public R saveBatch(@RequestBody Collection<DxhdAbry> entityList){
        Boolean result = dxhdAbryService.saveBatch(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("updateById")
    public R updateById(@RequestBody DxhdAbry entit){
        entit.setZp(null);
        entit.setZpDz(null);
        Boolean result = dxhdAbryService.updateById(entit);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("update")
    public R update(@RequestParam String xxbz,@RequestParam("file") MultipartFile file) throws IOException {
        DxhdAbry entity = new DxhdAbry();
        entity.setZp(file.getBytes());
        if(!StringUtils.isEmpty(xxbz) && !xxbz.equals("undefined")){
            entity.setXxbz(xxbz);
            dxhdAbryService.updateById(entity);
        }else{
            entity.setXxbz(UUID.randomUUID().toString());
            dxhdAbryService.save(entity);
        }
        return R.ok().put("data",entity.getXxbz());
    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("handleRemove")
    public R handleRemove(@RequestBody DxhdAbry entity) throws IOException {
        //DxhdAbry entity = new DxhdAbry();
        entity.setZp(new byte[0]);
        //entity.setXxbz(xxbz);
        dxhdAbryService.updateById(entity);
        return R.ok().put("data",entity.getXxbz());
    }


    @ApiOperation(value = "根据ID 批量更新")
    @RequestMapping("updateBatchById")
    public R updateBatchById(@RequestBody Collection<DxhdAbry> entityList){
        Boolean result = dxhdAbryService.updateBatchById(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 删除")
    @RequestMapping("removeById")
    public R removeById(@RequestBody DxhdAbry entity){
        Boolean result = dxhdAbryService.removeById(entity.getXxbz());
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "删除（根据ID 批量删除）")
    @RequestMapping("removeByIds")
    public R removeByIds(@RequestBody Collection<String> Ids){
        Boolean result = dxhdAbryService.removeByIds(Ids);
        return R.ok().put("data",result);
    }
}

